package com.yangguang.huahui.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 以shareprefence方式存储数据
 * 
 * @author linyg
 * 
 */
public class SharedPreferenceUtil {
	private static SharedPreferenceUtil sharedPreferenceUtil;
	private static SharedPreferences sharedPreferences;

	private final static String KEY = "huahui";
	public final static String TOKEN = "token";

	public final static String PHONE = "phone";
	public final static String PASSWORD = "password";
	public final static String IDENTITY = "identity";
	public final static String GUIDE = "guide";
	public final static String DATE = "date";
	public final static String NICKNAME = "nickname";
	public final static String SEX = "sex";
	public final static String SIGNATURE = "signature";
	public final static String AVATARS = "avatars";
	public final static String AREANEW = "areanew";
	public final static String TRADE = "trade";
	public final static String PROJECT_TYPE = "project_type";
	public final static String BANZU = "banzu";
	public final static String GOODSCATEGORY = "goodsCategory";
	public final static String MONTH_SALARY = "month_salary";
	public final static String DAY_SALARY = "day_salary";
	public final static String HOUR_SALARY = "hour_salary";
	public final static String WSTATUS = "wstatus";
	public final static String CHOUJIN = "chouojin";
	public final static String YG_LEIXING = "yg_leixing";
	public final static String AUTH_DATA = "auth_data";
	public final static String AUTHENTY = "authenty";
	public final static String PERSONINFO = "person_info";//工人，工头个人信息
	public final static String PERSONINFOTWO = "person_info_two";//业主，供应商个人信息
	public final static String LABEL = "label";//等级
	public final static String JIFEN = "jifen";//积分
	public final static String PERSONCENTER = "personCenter";
	public final static String CONTACT_PHONE = "contact_phone";
	public final static String CONTACT_PERSON = "contact_person";
	public final static String SERVICE_AREA = "service_area";
	public final static String SERVICE_AREA_ID = "service_area_id";
	public final static String TWID = "twid";
	public final static String TWIDID = "twidId";
	public final static String CITY = "city";
	public final static String SOUND = "sound";
	public final static String SHOCK = "shock";
	public final static String ISLOCATION = "is_location";
	public final static String JIJIA = "jijia";
	public final static String VERSION = "version";


	public final static String LANGUAGE = "language";
	public final static String HOME_GUIDE = "home_guide";
	public final static String MESSAGE_GUIDE = "message_guide";
	public final static String MY_GUIDE = "my_guide";

	private SharedPreferenceUtil(Context context) {
		sharedPreferences = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
	}

	public static SharedPreferenceUtil getInstance(Context context) {
		if (sharedPreferenceUtil == null) {
			sharedPreferenceUtil = new SharedPreferenceUtil(context);
		}
		return sharedPreferenceUtil;
	}

	/** 清空文件内容 */
	public void delete() {
		delete_item(TOKEN);
		delete_item(PHONE);
		delete_item(PASSWORD);
		delete_item(IDENTITY);
		delete_item(DATE);
		delete_item(AREANEW);
		delete_item(TRADE);
		delete_item(PROJECT_TYPE);
		delete_item(BANZU);
		delete_item(GOODSCATEGORY);
		delete_item(MONTH_SALARY);
		delete_item(DAY_SALARY);
		delete_item(HOUR_SALARY);
		delete_item(WSTATUS);
		delete_item(CHOUJIN);
		delete_item(YG_LEIXING);
		delete_item(AUTH_DATA);
		delete_item(AUTHENTY);
		delete_item(PERSONINFO);
		delete_item(PERSONINFOTWO);
		delete_item(LABEL);
		delete_item(JIFEN);
		delete_item(CONTACT_PHONE);
		delete_item(CONTACT_PERSON);
		delete_item(SERVICE_AREA);
		delete_item(SERVICE_AREA_ID);
		delete_item(CITY);
		delete_item(TWID);
		delete_item(TWIDID);
		delete_item(ISLOCATION);
		delete_item(JIJIA);
	}
	public void delete_item(String key){
		sharedPreferences.edit().remove(key).commit();
	}

	/**
	 * 设置String类型值
	 * 
	 * @param key
	 * @param value
	 * @time 2011-5-30 上午09:26:38
	 * @author:linyg
	 */
	public void putString(String key, String value) {
		Editor editor = sharedPreferences.edit();
		editor.putString(key, value);
		editor.commit();
	}

	/**
	 * 设置long类型值
	 * 
	 * @param key
	 * @param value
	 * @time 2011-5-30 上午09:28:25
	 * @author:linyg
	 */
	public void putLong(String key, long value) {
		Editor editor = sharedPreferences.edit();
		editor.putLong(key, value);
		editor.commit();
	}

	/**
	 * 设置int类型值
	 * 
	 * @param key
	 * @param value
	 * @time 2011-5-30 上午09:30:06
	 * @author:linyg
	 */
	public void putInt(String key, int value) {
		Editor editor = sharedPreferences.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	/**
	 * 设置Boolean类型值
	 * 
	 * @param key
	 * @param value
	 * @time 2011-5-30 上午09:30:56
	 * @author:linyg
	 */
	public void putBoolean(String key, boolean value) {
		Editor editor = sharedPreferences.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	/**
	 * 设置Float类型值
	 * 
	 * @param key
	 * @param value
	 * @time 2011-5-30 上午09:31:42
	 * @author:linyg
	 */
	public void putFloat(String key, float value) {
		Editor editor = sharedPreferences.edit();
		editor.putFloat(key, value);
		editor.commit();
	}

	/**
	 * 读取String类型值，默认为""
	 * 
	 * @param key
	 * @time 2011-5-30 上午09:33:01
	 * @author:linyg
	 */
	public String getString(String key) {
		return sharedPreferences == null ? "" : sharedPreferences.getString(key, "");
	}

	/**
	 * 读取boolean类型值，默认为false;
	 * 
	 * @param key
	 * @return
	 * @time 2011-5-30 上午09:33:54
	 * @author:linyg
	 */
	public boolean getBoolean(String key) {
		return sharedPreferences.getBoolean(key, true);
	}

	/**
	 * 读取int类型值，默认为0
	 * 
	 * @param key
	 * @return
	 * @time 2011-5-30 上午09:35:57
	 * @author:linyg
	 */
	public int getInt(String key) {
		return sharedPreferences.getInt(key, 0);
	}

	/**
	 * 读取long类型值，默认为0
	 * 
	 * @param key
	 * @return
	 * @time 2011-5-30 上午09:36:43
	 * @author:linyg
	 */
	public long getLong(String key) {
		return sharedPreferences.getLong(key, 0);
	}

	/**
	 * 读取float类型值，默认为0
	 * 
	 * @param key
	 * @return
	 * @time 2011-5-30 上午09:37:42
	 * @author:linyg
	 */
	public float getFloat(String key) {
		return sharedPreferences.getFloat(key, 0);
	}

	/** 判断是否存在此字段 */
	public boolean has(String key) {
		return sharedPreferences.contains(key);
	}


}
